package view;

import Dao.RalationalDataDao;
import Utils.DBUtils;
import jdk.nashorn.internal.scripts.JS;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;

public class mainFra extends JFrame{
    public JMenuBar menuBar;

    JTree tree;
    DefaultMutableTreeNode orgDataNode,rltDataNode;

    public JPanel nowPanel;

    public RalationalDataDao ralationalDataDao=new RalationalDataDao();

    Connection conn;
    private javax.swing.JPanel JPanel;

    public mainFra(Connection conn){
        this.conn=conn;
        //初始化
        setBounds(300,100,800,600);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JPanel.setLayout(new BorderLayout());
        setContentPane(JPanel);
        //创建菜单
//        createMenuBar();
        //主界面
//        desktopPane=new JDesktopPane();
//        desktopPane.setBackground(Color.GRAY);
//        add(desktopPane,BorderLayout.CENTER);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        //创建jtree
        createJTree();

        //tree控件监听
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                valueChangedPerfed();
            }
        });

    }

    //监听响应函数
    private void valueChangedPerfed() {
        DefaultMutableTreeNode selecteNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        if(selecteNode==orgDataNode||selecteNode==rltDataNode){
            return;
        }
        if(nowPanel!=null)
            remove(nowPanel);
        if(selecteNode==orgDataNode.getLastLeaf())
        {
            originalDataFrm originalDataFrm=new originalDataFrm(conn);
            nowPanel=(javax.swing.JPanel) originalDataFrm.getContentPane();
        }
        else {
            RelationFra relationFra = new RelationFra(conn, selecteNode.toString());
            nowPanel = (javax.swing.JPanel) relationFra.getContentPane();
        }
        add(nowPanel,BorderLayout.CENTER);
        setVisible(true);
    }

    //创建树
    private void createJTree() {
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("test");
        //创建二级节点
        orgDataNode = new DefaultMutableTreeNode("原始数据");
        rltDataNode = new DefaultMutableTreeNode("关系数据");

        // 把二级节点作为子节点添加到根节点
        rootNode.add(orgDataNode);
        rootNode.add(rltDataNode);

        //创建三级节点
        DefaultMutableTreeNode orgDataTable=new DefaultMutableTreeNode("原始数据表");
        orgDataNode.add(orgDataTable);

        //获取关系数据表名
        try {
            ResultSet set=ralationalDataDao.getTablesName(conn);
            while (set.next()){
                String itemName=set.getString("tablename");
                if(itemName.equalsIgnoreCase("orignalData")){
                    continue;
                }
                DefaultMutableTreeNode node=new DefaultMutableTreeNode(itemName);
                rltDataNode.add(node);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(e);
        }


        // 使用根节点创建树组件
        tree = new JTree();
        TreeModel treeModel = new DefaultTreeModel(rootNode);
        tree.setModel(treeModel);
        tree.setRootVisible(false);

        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(150,this.getHeight()));
        this.getContentPane().add(scrollPane,BorderLayout.WEST);
    }


    //创建菜单栏
//    private void createMenuBar() {
//        menuBar=new JMenuBar();
//        orignalData=new JMenu("原始数据");
//        ralationalData=new JMenu("关系数据");
//        //添加一级菜单
//        menuBar.add(orignalData);
//        menuBar.add(ralationalData);
        //获取关系数据表名
//        try {
//            ResultSet set=dbsDao.getTablesName(conn);
//            while (set.next()){
//                String itemName=set.getString("tablename");
//                if(itemName.equalsIgnoreCase("orignalData")){
//                    continue;
//                }
//                JMenuItem item=new JMenuItem(itemName);
//                ralationalData.add(item);
//                addActionLsner(item);
//            }
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(null,"数据库异常");
//            throw new RuntimeException(e);
//        }
//        setJMenuBar(menuBar);
//    }

    //建立监听函数
//    private void addActionListen(JMenuItem item){
//        item.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                RelationFra relationFra=new RelationFra(conn,item.getText());
////                desktopPane.add(relationFra);
//            }
//        });
//    }
}
